package com.hsmpay.mobile.action.forward

import com.hsmpay.common.pojo.user.User
import com.hsmpay.mobile.action.base.MobileBaseAction
import com.hsmpay.mobile.pojo.terminal.TerminalPrivileges
import org.apache.commons.lang.StringUtils
import org.apache.log4j.Logger
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Controller

import com.beust.klaxon.*

/**
 * 检查功能总接口  action
 */
@Controller("checkFunctionAction")
@Scope("prototype")
class CheckFunctionAction : MobileBaseAction() {

    private fun sendAppMsg(jsonObject: JsonObject) {
        // 查询密钥并返回数据
        val fc_des3_key = request.getAttribute(FC_DES3_Key) as String
        val message = encryptAppMsg(1, jsonObject.toJsonString().replace(":null",":\"\""), fc_des3_key)
        log.debug("-------------------响应json数据---------------------\n json=${jsonObject.toJsonString().replace(":null",":\"\"")}\n\n 加密后数据=$message\n----------------------------------------------")
        sendAppMsg(message)
    }

    /**
     * 查询可用功能列表
     */
    fun getVisibleFunction() {
        // 查询所有可见功能列表
        try {
            val tpl: List<TerminalPrivileges> = terminalPrivilegesService.searchVisibleFunctions()

            /**
             * {
             *     "reqCode": "0000",
             *     "visibleFunctions": [ "RECEIVABLES", "CASHMONEY", ... ]
             * }
             */
            val res = json {
                obj(
                        "reqCode" to "0000",
                        "visibleFunctions" to array(tpl.map {
                            it.functionKey
                        })
                )
            }
            sendAppMsg(res)
        } catch(ex: Exception) {
            log.debug(ex.message)
        }
    }

    /**
     * 检测功能

     * @throws Exception
     */
    @Throws(Exception::class)
    fun check() {
        val userId = java.lang.Long.parseLong(getJsonString("userId"))
        functionKey = getJsonString("functionKey")
        log.debug("functionKey = " + functionKey)
        if (StringUtils.isEmpty(functionKey)) {
            log.info("检测功能是否可用时, 传入参数functionKey为空")
            sendAppErrorMsg("6001", "functionKey")
            return
        }
        /**
         * {
         *     "reqCode": "0000",
         *     "reqMsg", ""
         * }
         */
        val tp: TerminalPrivileges = terminalPrivilegesService.searchPrivilegesByFunctionKey(functionKey)
        var res: JsonObject ? = null

        if (null == tp) {
            res = json {
                obj(
                        "reqCode" to "6002",
                        "reqMsg" to "亲,该功能正在努力开发中..."
                )
            }
            sendAppMsg(res)
            return
        }

        if (!checkPIV(functionKey)) {
            log.info("功能检测失败, " + tp.name + " 功能暂停使用！")
            return
        }

        val user1 = User()
        user1.id = userId
        user1.deleted = 0
        val user = userService.searchEntity(user1)

        if (user == null) {
            res = json {
                obj(
                        "reqCode" to "6004",
                        "reqMsg" to "用户：$userId 该用户被销户或暂停使用！",
                        "channelId" to tp.payTchannel
                )
            }
        } else {
            if ("RECEIVABLES" == functionKey) {
                res = json {
                    obj(
                            "reqCode" to "0000",
                            "channelId" to user.channelId
                    )
                }
            } else {
                res = json {
                    obj(
                            "reqCode" to "0000",
                            "channelId" to tp.payTchannel
                    )
                }
            }
        }

        sendAppMsg(res)
    }

    companion object {
        private val serialVersionUID = 3227967920972760714L
        private val log = Logger.getLogger(CheckFunctionAction::class.java)
    }
}
